package com.zjdiepu.www.partner.api.hzicbc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.diepu.www.client.partner.proxy.request.ApplyDiviBackProxyRequest;
import com.diepu.www.client.partner.proxy.request.ApplyDiviStatusProxyRequest;
import com.diepu.www.client.partner.proxy.request.OpenCardbackReasonRequest;
import com.diepu.www.client.partner.proxy.request.UpdateDownloadStatusRequest;
import com.diepu.www.client.partner.proxy.request.UpdatePushStatusRequest;
import com.diepu.www.client.partner.proxy.response.DiepuYwInterviewInfo;
import com.diepu.www.client.partner.proxy.response.SynInterviewInfoResponse;
import com.diepu.www.client.proxy.partner.common.DiepuOtherAttachments;
import com.diepu.www.client.proxy.partner.common.DiepuOtherAttachmentsResponse;
import com.diepu.www.client.proxy.partner.request.own.DiepuOtherAttachmentsRequest;
import com.diepu.www.client.proxy.partner.request.own.DiepuYwOpenCardHzicbcRequest;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.ibiz.icbc.IDzApplyDiviService;
import com.zjdiepu.www.ibiz.icbc.IGetAtttachmentsService;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcService;
import com.zjdiepu.www.ibiz.icbc.IYwOrdernoBankService;
import com.zjdiepu.www.ibiz.yw.IYwInterviewInfoService;
import com.zjdiepu.www.model.base.dto.OtherAttachments;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbc;
import com.zjdiepu.www.request.BaseRequest;
import com.zjdiepu.www.response.BaseResponse;
import com.zjdiepu.www.response.BooleanResponse;
import com.zjdiepu.www.security.exception.BasePartnerException;
import com.zjdiepu.www.security.server.convert.MyJson;
import com.zjdiepu.www.security.server.core.Partners;
import com.zjdiepu.www.security.server.interceptor.PartnerProvider;
/**
 * 类描述:杭州工商银行接口<br/> 
 * 项目名称:diepu-parent-partner <br/> 
 * 类名称:XinHeHuiController <br/>     
 * 创建人:xiongxiaotun <br/> 
 * 创建时间:2017年8月15日 下午5:56:32  <br/>   
 * 修改人:xiongxiaotun <br/> 
 * 修改时间: 2017年8月15日 下午5:56:32   <br/>  
 * 修改备注:   <br/> 
 * @version V1.0   <br/>
 */
@MyJson
@RestController
@RequestMapping(value="HzicbcController", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = { "杭州工商银行接口" }, produces = "application/json;charset=UTF-8")
@PartnerProvider(partner = Partners.HZ_ICBC)
public class HzicbcController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private IDzApplyDiviService dzApplyDiviService;
	
	@Resource
    private IYwOpenCardHzicbcService ywOpenCardHzicbcService;
	
	@Resource
	private IGetAtttachmentsService getAttachmentsService;
	
	@Resource
	private IYwOrdernoBankService ywOrdernoBankService;
	
	@Resource
	private IYwInterviewInfoService ywInterviewInfoService;
	
	/**
	 * 更新开卡状态
	 * @return
	 */
	@ApiOperation(value = "更新开卡状态", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("updateOpenCardStatus")
	public BaseResponse<BooleanResponse> updateOpenCardStatus(@Validated @RequestBody DiepuYwOpenCardHzicbcRequest diepuYwOpenCardHzicbcRequest){
		YwOpenCardHzicbc ywOpenCardHzicbc=new YwOpenCardHzicbc();
		BeanUtils.copyProperties(diepuYwOpenCardHzicbcRequest, ywOpenCardHzicbc);
		ywOpenCardHzicbcService.updateHzicbcOpencardStatus(ywOpenCardHzicbc);
		return BaseResponse.successWithBoolean(true);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: synInterviewInfo <br/>
	 * @Description: 同步面签信息 <br/>
	 * @param baseRequest
	 * @return
	 * @version V1.0 <br/>
	 * @throws BasePartnerException 
	 */
	@ApiOperation(value = "同步面签信息", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("synInterviewInfo")
	public BaseResponse<SynInterviewInfoResponse> synInterviewInfo(@Validated @RequestBody BaseRequest baseRequest) throws BasePartnerException {
		List<DiepuYwInterviewInfo> interviewInfos = ywInterviewInfoService.synInterviewInfo();
		SynInterviewInfoResponse response = new SynInterviewInfoResponse();
		response.setInterviewInfos(interviewInfos);
		return BaseResponse.success(response);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: pushStatus <br/>
	 * @Description: 同步面签视频:推送状态 <br/>
	 * @param updatePushStatusRequest
	 * @return
	 * @version V1.0 <br/>
	 * @throws BasePartnerException 
	 */
	@ApiOperation(value = "同步面签视频:推送状态", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("pushStatus")
	public BaseResponse<BooleanResponse> pushStatus(@Validated @RequestBody UpdatePushStatusRequest updatePushStatusRequest) throws BasePartnerException {
		ywInterviewInfoService.savePushStatus(updatePushStatusRequest.getBusiId(), updatePushStatusRequest.getPushStatus());
		return BaseResponse.successWithBoolean(true);
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: downloadStatus <br/>
	 * @Description: 同步面签视频:下载状态 <br/>
	 * @param updateDownloadStatusRequest
	 * @return
	 * @version V1.0 <br/>
	 * @throws BasePartnerException 
	 */
	@ApiOperation(value = "同步面签视频:下载状态", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("downloadStatus")
	public BaseResponse<BooleanResponse> downloadStatus(@Validated @RequestBody UpdateDownloadStatusRequest updateDownloadStatusRequest) throws BasePartnerException {
		ywInterviewInfoService.saveDownloadStatus(updateDownloadStatusRequest.getBusiId(), updateDownloadStatusRequest.getDownloadStatus(), updateDownloadStatusRequest.getUrlLocal());
		return BaseResponse.successWithBoolean(true);
	}
	
	/**
	 * 银行申请分期退回
	 * @param ApplyDiviBackProxyRequest
	 * @return
	 */
	@ApiOperation(value = "银行申请分期退回", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("applyDiviBack")
	public BaseResponse<BooleanResponse> applyDiviBack(@Validated @RequestBody ApplyDiviBackProxyRequest request) {
		logger.info("HzicbcController.applyBack start.");
		return dzApplyDiviService.saveApplyDiviBack(request);
	}
	/**
	 * 银行申请分期状态通知
	 * @param ApplyDiviStatusProxyRequest
	 * @return
	 */
	@ApiOperation(value = "银行申请分期状态通知", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("applyDiviStatus")
	public BaseResponse<BooleanResponse> applyDiviStatus(@Validated @RequestBody ApplyDiviStatusProxyRequest request) {
		logger.info("HzicbcController.applyDiviStatus start.");
		return dzApplyDiviService.saveApplyDiviStatus(request);
	}
	
	@ApiOperation(value = "开卡退回理由", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("openCardBackReason")
	public BaseResponse<BooleanResponse> openCardBackReason(
			@Validated @RequestBody OpenCardbackReasonRequest backNoticeRequest) {
		boolean flag=false;
		if(StringUtils.isNotEmpty(backNoticeRequest.getBusiId())){
			YwOpenCardHzicbc ywOpenCardHzicbc=new YwOpenCardHzicbc();
			ywOpenCardHzicbc.setBusiId(backNoticeRequest.getBusiId());
			ywOpenCardHzicbc.setDpStatus(DpStatus.NORMAL.getCode());
			ywOpenCardHzicbc=ywOpenCardHzicbcService.selectRefined(ywOpenCardHzicbc);
			if(ywOpenCardHzicbc!=null){
				ywOpenCardHzicbc.setReasonBack(backNoticeRequest.getBacknote());
				ywOpenCardHzicbc.setOpenCardStatus(backNoticeRequest.getOpenCardStatus());
				flag=ywOpenCardHzicbcService.updateOpenCardBackReason(ywOpenCardHzicbc);
				return BaseResponse.successWithBoolean(flag);
			}
		}
		return BaseResponse.successWithBoolean(flag);
	}
	
	/**
	 * 开卡成功通知
	 * @return
	 */
	@ApiOperation(value = "开卡成功通知", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("openCardSuccess")
	public BaseResponse<BooleanResponse> openCardSuccess(@Validated @RequestBody DiepuYwOpenCardHzicbcRequest diepuYwOpenCardHzicbcRequest){
		YwOpenCardHzicbc ywOpenCardHzicbc=new YwOpenCardHzicbc();
		BeanUtils.copyProperties(diepuYwOpenCardHzicbcRequest, ywOpenCardHzicbc);
		ywOpenCardHzicbcService.saveOpenCardSuccess(ywOpenCardHzicbc);
		return BaseResponse.successWithBoolean(true);
	}
	
	@ApiOperation(value = "中转项目调用主程序获取附件列表", response = BaseResponse.class, httpMethod = "POST")
	@RequestMapping("getOtherAttachments")
	public BaseResponse<DiepuOtherAttachmentsResponse> getOtherAttachments(@Validated @RequestBody DiepuOtherAttachmentsRequest diepuOtherAttachmentsRequest){
		List<OtherAttachments> list=getAttachmentsService.getAttachmentsByDataId(diepuOtherAttachmentsRequest.getDataId());
		DiepuOtherAttachmentsResponse response=new DiepuOtherAttachmentsResponse();
		List<DiepuOtherAttachments> list1=new ArrayList<DiepuOtherAttachments>();
		DiepuOtherAttachments diepuOtherAttachments=null;
		for (OtherAttachments otherAttachments : list) {
			diepuOtherAttachments=new DiepuOtherAttachments();
			BeanUtils.copyProperties(otherAttachments, diepuOtherAttachments);
			list1.add(diepuOtherAttachments);
		}
		response.setOtherAttachmentsList(list1);
		return BaseResponse.success(response);
		
	}
}
